layui.use(['layer', 'form', 'admin', 'laydate', 'ax', 'formSelects', 'func'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;
    var laydate = layui.laydate;
    var layer = layui.layer;
    var formSelects = layui.formSelects;
    var func = layui.func;

    /**
     * 初始化表单，要加上，不然刷新部分组件可能会不加载
     */
    form.render();

    //初始化
    $(function(){
        var bannerId = $("#bannerId").val();
        if(bannerId != '' && bannerId != null && bannerId != undefined){
            //获取详情信息
            var ajax = new $ax(Feng.ctxPath + "/rw/banner/getInfoById/" + bannerId);
            var result = ajax.start();
            $("#showImage").html('<img src="/hyRedWineFile/images/' + result.fileName + '">');
            form.val('bannerForm', result);
        }
    });

    //点击选择图片按钮
    $("#imageBtn").on("click", function(){
        top.layui.admin.open({
            type: 2,
            title: "选择图片",
            maxmin: false,
            area: ['100%', '100%'],
            offset: '0',
            content: Feng.ctxPath + '/rw/images/imagesIndex',
            end: function(index, layero){

                var imagesId = admin.getTempData('imagesId');
                var fileName = admin.getTempData('fileName');

                if(imagesId != null && imagesId != '' && imagesId != undefined){
                    $("#showImage").html('<img src="/hyRedWineFile/images/' + fileName + '">');
                    $("#imageId").val(imagesId);
                    admin.putTempData('imagesId', '');
                    admin.putTempData('fileName', '');
                }
            },
            cancel: function(index, layero){
                admin.putTempData('imagesId', '');
                admin.putTempData('fileName', '');
            }
        });

        return true;
    });

    // 表单提交事件
    form.on('submit(btnSubmit)', function (data) {
        var ajax = new $ax(Feng.ctxPath + "/rw/banner/save", function (data) {
            Feng.success("保存成功！");

            //传给上个页面，刷新table用
            admin.putTempData('formOk', true);

            //关掉对话框
            admin.closeThisDialog();

        }, function (data) {
            Feng.error("保存失败！" + data.responseJSON.message)
        });
        ajax.set(data.field);
        ajax.start();

        //添加 return false 可成功跳转页面
        return false;
    });
});